/*
 * @Author: huangyingli
 * @Date: 2022-10-20 15:26:38
 * @LastEditors: huangyingli
 * @LastEditTime: 2022-12-08 16:24:54
 * @Description:
 */
const { camel } = require('naming-style');
function resSuccess(data) {
  return {
    code: 200,
    msg: '成功',
    status: true,
    data: nameTransform(data),
  };
}

function resError(error) {
  return {
    code: 500,
    msg: error || '失败',
    status: false,
    data: null,
  };
}

function nameTransform(data) {
  let result;
  if (Array.isArray(data)) {
    result = [];
    data.forEach((d) => {
      let obj = {};
      for (key in d) {
        obj[camel(key)] = d[key];
      }
      result.push(obj);
    });
  } else {
    result = {};
    for (key in data) {
      result[camel(key)] = data[key];
    }
  }

  return result;
}

module.exports = {
  resSuccess,
  resError,
  nameTransform
};
